package com.wantupai.translate.activity;

import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jianjin.camera.utils.UriUtils;
import com.jianjin.camera.widget.CameraContainer;
import com.jianjin.camera.widget.CameraManager;
import com.jianjin.camera.widget.ISavePicCallback;
import com.wantupai.translate.R;
import com.wantupai.translate.base.BaseActivity;
import com.wantupai.translate.utils.DataCleanUtil;
import com.wantupai.translate.utils.MyLog;
import com.wantupai.translate.utils.ToastUtils;

import static com.wantupai.translate.Constant.ImagePath;

/**
 * @author luxuchang
 * @date 2019/4/23 0023
 * @description 拍照页面
 */
public class TakePictureActivity extends BaseActivity implements ISavePicCallback {
    private CameraManager mCameraManager;
    private CameraContainer cameraContainer;
    private TextView mCameraIvLight;
    private TextView mCameraTvSwitch;
    private Button takePic;
    private long time = 0L;

    @Override
    public int getLayoutId() {
        return R.layout.activity_take_picture;
    }

    @Override
    protected boolean translucentStatusBar() {
        return true;
    }

    @Override
    public void initViews() {
        cameraContainer = findViewById(R.id.camera_container);
        mCameraIvLight = findViewById(R.id.btn_torch);
        mCameraTvSwitch = findViewById(R.id.btn_switch_camera);
        takePic = findViewById(R.id.takePic);

        cameraContainer.findViewById(R.id.iv_focus).setVisibility(View.GONE);
        mCameraManager = CameraManager.getInstance(this);
        mCameraManager.bindOptionMenuView(mCameraIvLight, mCameraTvSwitch, null, null);
        cameraContainer.bindActivity(this);

    }

    @Override
    public void initListener() {
        takePic.setOnClickListener(this);
        mCameraTvSwitch.setOnClickListener(this);
        mCameraIvLight.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    public void processClick(View v) {
        switch (v.getId()) {
            case R.id.takePic:
                cameraContainer.takePicture(TakePictureActivity.this);
                break;
            case R.id.btn_switch_camera:
                cameraContainer.switchCamera();
                break;
            case R.id.btn_torch:
                cameraContainer.switchFlashMode();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int req, int res, Intent data) {
        if (res == RESULT_OK) {
            try {
                //该uri是上一个Activity返回的
                Uri uri = data.getData();

//                File file = new FileStorage().createIconFile(); //工具类
//                if (Build.VERSION.SDK_INT >= 24) {
//                    // 针对Android7.0，需要通过FileProvider封装过的路径，提供给外部调用
//                    // 通过FileProvider创建一个content类型的Uri，进行封装
//                    Uri imageUri = FileProvider.getUriForFile(activity, "com.pecoo.ifcoo.fileprovider", file);
//                    output = file;
//                } else {
                    // 7.0以下，如果直接拿到相机返回的intent值，拿到的则是拍照的原图大小，很容易发生OOM，所以我们同样将返回的地址，保存到指定路径，返回到Activity时，去指定路径获取，压缩图片
//                    output = ImageUtils.createFile(FileUtils.getInst().getPhotoPathForLockWallPaper(), true);
//                    Uri imageUri = Uri.fromFile(output);
//                }
                if (uri != null) {
                    Intent intent = new Intent();
                    intent.setAction("com.wantupai.app.picactivity");
                    intent.putExtra("imgUri", UriUtils.getPath(TakePictureActivity.this, uri));
                    startActivity(intent);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (cameraContainer != null) {
            cameraContainer.onStart();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (cameraContainer != null) {
            cameraContainer.onStop();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCameraManager.unbindView();
        cameraContainer.releaseCamera();
    }

    @Override
    public void saveComplete(String s) {
        startActivity(new Intent(TakePictureActivity.this, PicActivity.class).putExtra("picPath", s));
    }

    @Override
    public void saveFailure(String s) {
        MyLog.e("TakePictureActivity", "saveFailure:" + s);
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - time > 2000) {
                ToastUtils.show(getString(R.string.main_activity_exit_msg));
                time = System.currentTimeMillis();
            } else {
                DataCleanUtil.cleanCustomCache(ImagePath);
                finish();
                System.exit(0);
            }
            return true;
        }
        //退出整个应用程序
        return super.onKeyDown(keyCode, event);
    }
}
